package com.tonghe.handler.security;

import com.alibaba.fastjson.JSON;
import com.tonghe.domain.ResponseResult;
import com.tonghe.enums.AppHttpCodeEnum;
import com.tonghe.utils.WebUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Date:2022/2/22- 02- 22- 17:01
 * 鉴权 异常 Handler
 **/
@Component // 注入spring容器中
public class AccessDenieHandlerImpl implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accEx) throws IOException, ServletException {
        accEx.printStackTrace();
        ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);//403无权限
        WebUtils.renderString(response, JSON.toJSONString(result));//响应给前端

    }
}
